/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "ssi_dataexception.h"
#include "ssi_functiondecl.h"
#include "ssi_datatypes.h"
#include "ssi_functionapi.h"


SSI_INT64 CDataException::sType = 0;

CDataException::CDataException(void)
{
  m_Status = STAT_OK;
  
  if(!sType){
		sType = 1;
		sType = Factory_RegisterData((CData*)new CDataException());
	}
	Type = sType;
	DataName = "exception";
}

CDataException::~CDataException(void)
{
}

SSI_BOOL CDataException::CopyData(CData* Other)
{
	if(Other == SSI_NULL) return 0;
  if(Other->GetType() != Type) return SSI_NULL;
	CDataException* In = (CDataException*)Other;
	m_Status = In->m_Status;
  return 1;
}

CData* CDataException::MakeData(void)
{
	return (CData*) new CDataException();
}

SSI_BOOL CDataException::DeleteData(CData* In)
{
	delete ((CDataException*)In);
	return 1;
}

SSI_BOOL CDataException::DataFromString(const String& sData)
{
	return 0;
}

String CDataException::GetString(void)
{
	String Ret;
  Ret.Format("[State: %d] %s", m_Status.GetState(), m_Status.GetStatusMessage().Str());
  
	return Ret;
}

SSI_INT64 CDataException::SizeOf(void)
{
	return 0;
}

CData* CDataException::operator_equal(CData* InData, OpInfoT* Info)
{
	CopyData(InData);
	return (CData*)this;
}

SSI_INT64 CDataException::GetRegisteredType(void)
{
	return sType;
}

SSI_INT64 CDataException::Register(SSI_INT64 NewType){
	sType = NewType;
	return 1;
}

void CDataException::UnRegister(){
	sType = 0;
}

CStatus CDataException::InitData(void)
{
	CFunctionDecl aNewFunc;
  
	aNewFunc.Name = "throw";
	aNewFunc.SetProcessor(this);
	AddFunction(aNewFunc);

  aNewFunc.Clear();
	aNewFunc.Name = "GetState";
  aNewFunc.ReturnType = CDataInt::GetRegisteredType();
	aNewFunc.SetProcessor(this);
	AddFunction(aNewFunc);

  aNewFunc.Clear();
	aNewFunc.Name = "GetMessage";
  aNewFunc.ReturnType = CDataString::GetRegisteredType();
	aNewFunc.SetProcessor(this);
	AddFunction(aNewFunc);

  aNewFunc.Clear();
	aNewFunc.Name = "Set";
  aNewFunc.addType(CDataInt::GetRegisteredType());
  aNewFunc.addType(CDataString::GetRegisteredType());
	aNewFunc.SetProcessor(this);
	AddFunction(aNewFunc);

  aNewFunc.Clear();
	aNewFunc.Name = "SetError";
  aNewFunc.addType(CDataString::GetRegisteredType());
	aNewFunc.SetProcessor(this);
	AddFunction(aNewFunc);

  aNewFunc.Clear();
	aNewFunc.Name = "SetWarning";
  aNewFunc.addType(CDataString::GetRegisteredType());
	aNewFunc.SetProcessor(this);
	AddFunction(aNewFunc);

  aNewFunc.Clear();
	aNewFunc.Name = "SetFailure";
  aNewFunc.addType(CDataString::GetRegisteredType());
	aNewFunc.SetProcessor(this);
	AddFunction(aNewFunc);

  aNewFunc.Clear();
	aNewFunc.Name = "SetOK";
  aNewFunc.addType(CDataString::GetRegisteredType());
	aNewFunc.SetProcessor(this);
	AddFunction(aNewFunc);





  return STAT_OK;
}

void CDataException::SSI_FunctionProcessor(SSI_FunctionAPI& theAPI)
{
	CStatus RetStat;
	if(theAPI.m_FunctionName == "throw"){
    theAPI.m_ReturnStatus = m_Status;
		return;
	}else	if(theAPI.m_FunctionName == "GetState"){
		CDataInt ret;
    ret.Data = m_Status.GetState();
    theAPI.SetReturnData(&ret);
		return;
	}else	if(theAPI.m_FunctionName == "GetMessage"){
		CDataString ret;
    ret.Data = m_Status.GetStatusMessage();
    theAPI.SetReturnData(&ret);
		return;
	}else	if(theAPI.m_FunctionName == "Set"){
    SSI_INT64 iStat = SSI_EXTRACT(CDataInt, 0);
    m_Status.Set(iStat, SSI_EXTRACT(CDataString, 1));
		return;
	}else	if(theAPI.m_FunctionName == "SetError"){
    m_Status.Set(sERROR, SSI_EXTRACT(CDataString, 0));
		return;
	}else	if(theAPI.m_FunctionName == "SetWarning"){
    m_Status.Set(sWARNING, SSI_EXTRACT(CDataString, 0));
		return;
	}else	if(theAPI.m_FunctionName == "SetFailure"){
    m_Status.Set(sFAIL, SSI_EXTRACT(CDataString, 0));
		return;
	}else	if(theAPI.m_FunctionName == "SetOK"){
    m_Status.Set(sOK, SSI_EXTRACT(CDataString, 0));
		return;
	}

}

void CDataException::SetStatus(const CStatus& In)
{
  m_Status = In;
}

CStatus CDataException::GetStatus()
{
  return m_Status;
}




